#!/bin/bash

# 安装脚本 - GWAC FIT图像监控系统

CONFIG_FILE="/home/gwac/dwl/cut_monitor/data_monitor/config.json"
SERVICE_FILE="/etc/systemd/system/gwac_fit_monitor.service"
MONITOR_DIR="/home/gwac/dwl/cut_monitor/data_monitor"

# 检查Python版本
python_version=$(python3 -c 'import sys; print(sys.version_info >= (3, 6))')
if [ "$python_version" != "True" ]; then
    echo "错误: 需要Python 3.6或更高版本"
    exit 1
fi

# 检查astropy是否安装
if ! python3 -c "import astropy" &> /dev/null; then
    echo "安装astropy包..."
    pip3 install astropy
fi

# 创建配置目录
mkdir -p "$MONITOR_DIR"

# 设置配置文件
echo "配置观测目录路径..."
read -p "请输入相机服务器观测目录的根路径: " obs_root_path

cat > "$CONFIG_FILE" <<EOL
{
    "observation_root": "$obs_root_path",
    "processing_root": "/data/work/gwac_diff_data_new",
    "output_dir": "/home/gwac/dwl",
    "scan_interval": 1,
    "log_file": "/home/gwac/dwl/fit_monitor.log",
    "max_retries": 3,
    "retry_delay": 5
}
EOL

# 设置服务文件
echo "安装systemd服务..."
sudo cp "$MONITOR_DIR/gwac_fit_monitor.service" "$SERVICE_FILE"
sudo systemctl daemon-reload

# 设置权限
sudo chown -R gwac:gwac "$MONITOR_DIR"
sudo chmod 644 "$CONFIG_FILE"
sudo chmod 644 "$SERVICE_FILE"
sudo chmod +x "$MONITOR_DIR/fit_image_monitor.py"

echo "安装完成！"
echo "可以使用以下命令管理服务:"
echo "启动服务: sudo systemctl start gwac_fit_monitor"
echo "停止服务: sudo systemctl stop gwac_fit_monitor"
echo "查看状态: sudo systemctl status gwac_fit_monitor"
echo "启用开机启动: sudo systemctl enable gwac_fit_monitor"